package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.DoctorDTO;
import com.pshcp.domain.dto.DoctorIntroDTO;
import com.pshcp.domain.model.Doctor;
import com.pshcp.mapper.DoctorMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 医生信息表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorService {

    @Autowired
    private DoctorMapper doctorMapper;

    /**
     * 自增在线咨询单量
     * @param id
     */
    public void updateOnlineNumById(Long id){
        doctorMapper.updateOnlineNumById(id);
    }

    /**
     * 医生介绍列表
     * @param
     * @return
     */
    public PageList<Doctor> doctorIntroList(DoctorIntroDTO doctorIntroDTO){
        Map<String, Object> params = new HashMap<>();
        params.put("search", doctorIntroDTO.getSearch());
        params.put("deptDescId", doctorIntroDTO.getDeptDescId());
        params.put("titleShown", doctorIntroDTO.getTitleShown());
        if (doctorIntroDTO.getPageNum() != null && doctorIntroDTO.getPageSize() != null) {
            PageHelper.startPage(doctorIntroDTO.getPageNum(), doctorIntroDTO.getPageSize());
        }
        List<Doctor> list = doctorMapper.doctorIntroList(params);
        return CommonTool.buildPageList(list);
    }

    public Doctor getDoctorByPhone(String phone) {
        Example example = new Example(Doctor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", phone);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        List<Doctor> list = doctorMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public PageList<Doctor> list(DoctorDTO doctorDTO) {
        if (doctorDTO.getPageNum() != null && doctorDTO.getPageSize() != null) {
            PageHelper.startPage(doctorDTO.getPageNum(), doctorDTO.getPageSize());
        }
        Example example = new Example(Doctor.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorDTO.getStatus());
        }else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if(!ObjectUtils.isEmpty(doctorDTO.getIds())){
            criteria.andIn("id", doctorDTO.getIds());
        }
        if(!ObjectUtils.isEmpty(doctorDTO.getDeptId())){
            criteria.andEqualTo("mainDeptId", doctorDTO.getDeptId());
        }
        if(doctorDTO.getMediLevel() != null){
            criteria.andEqualTo("mediLevel", doctorDTO.getMediLevel());
        }
        if(!ObjectUtils.isEmpty(doctorDTO.getTitleShown())){
            criteria.andEqualTo("titleShown", doctorDTO.getTitleShown());
        }
        if(!ObjectUtils.isEmpty(doctorDTO.getSearch())){
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.orLike("name", "%"+doctorDTO.getSearch()+"%");
            criteria1.orLike("mainDeptName", "%"+doctorDTO.getSearch()+"%");
            example.and(criteria1);
        }
        //0综合排序(咨询价格最低+咨询量最高) 1咨询量多
        if("0".equals(doctorDTO.getSortType())){
            example.setOrderByClause(" online_fee asc, online_num desc");
        }if("1".equals(doctorDTO.getSortType())){
            example.setOrderByClause(" online_num desc");
        }else{
            example.setOrderByClause(" created_time desc");
        }
        List<Doctor> list = doctorMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<Doctor> select(Doctor doctor) {
        return doctorMapper.select(doctor);
    }

    public int insert(Doctor doctor) {
        doctor.setId(SystemUtil.getId());
        doctor.setStatus(CommonStatusEnum.INIT.getCode());
        doctor.setCreatedTime(new Date());
        return doctorMapper.insert(doctor);
    }

    public int insertList(List<Doctor> doctors){
        doctors.forEach(o->{
            o.setId(SystemUtil.getId());
            o.setStatus(CommonStatusEnum.INIT.getCode());
            o.setCreatedTime(new Date());
        });
        return doctorMapper.insertList(doctors);
    }

    public int update(Doctor doctor) {
        doctor.setUpdateTime(new Date());
        return doctorMapper.updateByPrimaryKeySelective(doctor);
    }

    public Doctor findById(Long id) {
        return doctorMapper.selectByPrimaryKey(id);
    }

    public List<Doctor> findByIds(List<Long> ids) {
        Example example = new Example(Doctor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorMapper.selectByExample(example);
    }

    public Doctor findByHisDoctorId(String hisDoctorId) {
        Example example = new Example(Doctor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("hisDoctorId", hisDoctorId);
        List<Doctor> doctors =  doctorMapper.selectByExample(example);
        return CollectionUtils.isEmpty(doctors) ? null : doctors.get(0);
    }


    public List<Doctor> findByHisDoctorIds(List<String> hisDoctorIds) {
        Example example = new Example(Doctor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("hisDoctorId", hisDoctorIds);
        List<Doctor> doctors =  doctorMapper.selectByExample(example);
        return doctors;
    }

    public int delById(Long id) {
        Example example = new Example(Doctor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        Doctor doctor = new Doctor();
        doctor.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorMapper.updateByExampleSelective(doctor, example);
    }

}
